main
Sub Main
	dim args
	args = inputbox("x y xn yn",args,"20 20 2 2")
	args = trim(args)
	if args="" then 
		msgbox "wrong input"
		exit sub
	end if
	
	dim s,ls,i,j
	s = RegexSplit(args," ")
	ls = ubound(s)-lbound(s)+1
	For i = 0 To UBound(s)
		'msgbox s(i)
	next
	'msgbox ls
	if ls <> 4 then 
		msgbox "wrong input"
		exit sub
	end if
	
	dim dx,dy,nx,ny
	dx = cdbl(s(0))
	dy = cdbl(s(1))
	nx = cint(s(2))
	ny = cint(s(3))
	Dim doc
	Set doc = Application.ActiveDocument	
	dim obj,objs
	Set objs = doc.GetObjects(4,,True)
	
	dim tmpfile
	tmpfile = doc.path & "\tmp1.mcr"
	
	dim unit,unit_str
	unit = doc.Unit
	select case unit
		case 2'mil
			unit_str = "mil"
		case 4'mm
			unit_str = "mm"
		case else
			msgbox "please switch units to mm or mils"
			exit sub
	end select
	
	
	dim ostr
	dim x,y
	
	dim x1,y1
	if objs.count=1 then
		for each obj in objs
			x = obj.PositionX
			y = obj.PositionY	
			ostr = "Application.ExecuteCommand(""Edit Copy"")" 
			for i= 0 to nx-1	
				x1 = x+ i * dx
				for j= 0 to ny-1					
					y1 = y + j * dy
					if x1=x and y1 = y then
					else
						ostr = ostr & chr(13) & chr(10)
						ostr = ostr & "MainView.MouseMove(" & x1 & unit_str &", " & y1 & unit_str & ", ""C"")" & chr(13) & chr(10)
						ostr = ostr & "Application.ExecuteCommand(""Complete Move"", " & x1 & unit_str &", " & y1 & unit_str & ")"
					end if
				next				
			next			
			ostr = ostr & vbcrlf & "Application.ExecuteCommand(""Cancel"")"			
			'msgbox tmpfile
			'msgbox ostr
			Dim fso,fw
			Set fso = CreateObject("scripting.filesystemobject")
			Set fw = fso.CreateTextFile(tmpfile,True)
			fw.Write ostr
			fw.close
			Set fw = Nothing	
			Set fso = Nothing	
			'msgbox tmpfile
			application.RunMacro(tmpfile,"")
			'Dim ws
			'Set ws = CreateObject("WScript.Shell")
			'ws.run tmpfile
			'set ws = nothing	
		next
	else
		MsgBox "Please select only one Vias to Execute!"
	end if
End Sub

Function RegexSplit(expression, delimiter)
	dim isGotoTheEnd : isGotoTheEnd = false
	
	Dim regex, matches, arr(), i
	dim default_delimiter : default_delimiter= "@"
	dim tmpstr :tmpstr = "###!!!###!!!"
	if delimiter=default_delimiter then	
	else
		if instr(expression,default_delimiter)=0 then
			expression = replace(expression,delimiter,default_delimiter)
		else
			if instr(expression,tmpstr)=0 then
				expression = replace(expression,default_delimiter,tmpstr)
				expression = replace(expression,delimiter,default_delimiter)
			else
				msgbox "UnSupport ( conflict string )"
				ReDim arr(0)
				arr(0) = expression
				isGotoTheEnd = true
			end if			
		end if	
	end if
	'Continue	
	if isGotoTheEnd = false then
		dim len_expression :len_expression =len(expression)		
		Set regex = CreateObject("VBScript.RegExp") 
		regex.Pattern = default_delimiter
		regex.Global = True
		Set matches = regex.Execute(expression)
		dim ncount
		ncount = matches.Count+1
		ReDim arr(matches.Count)
		If ncount = 0 Then
			arr(0) = expression			
		else
			dim l_index,u_index,ul_len
			l_index = 0	
			For i = 0 To matches.Count - 1
				u_index = matches(i).FirstIndex-1
				ul_len = u_index-l_index+1
				arr(i) = Mid(expression, l_index, ul_len)
				l_index = matches(i).FirstIndex+1
			next
			'last item
			ul_len=len_expression - l_index
			arr(i) = Mid(expression, l_index, ul_len)
		end if
		'The End
		For i = 0 To UBound(arr)
			arr(cint(i)) = replace(arr(cint(i)),default_delimiter,delimiter)
			'msgbox arr(cint(i))
		Next
	end if	
    RegexSplit = arr
End Function
